// 获取任务号树
const IgetTaskNumberTree = 'a/workorder/workorder/tasklist'
// 获取工单树
const IgetWorkOrderTree = 'a/workorder/workorder/getworkordertree'
// 通过工单号查找任务号
const IgetTaskNumber = 'a/workorder/workorder/gettasknumber'

/**
 * @author liubaohui
 * @description 树请求
 */
let TreeService = {
  methods: {
    /**
     * 获取任务号树
     * @returns {Promise<Array>}
     */
    getTaskNumberTree(query = {}) {
      // return mockTaskTree
      this.isLoading = true
      let req = {
        method: 'post',
        url: IgetTaskNumberTree,
        data: query,
        noCommit: true
      }
      return this.$store.dispatch('saveData', req)
        .then(res => {
          return res.data.tasklist
        }).catch(() => {
          return []
        }).finally(() => {
          this.isLoading = false
        })
    },
    /**
     * 获取工单树
     * @param taskNumber
     * @returns {Promise<any>}
     */
    getWorkOrderTree(taskNumber) {
      let req = {
        method: 'post',
        url: IgetWorkOrderTree,
        data: {
          taskNumber
        },
        noCommit: true
      }
      return this.$store.dispatch('saveData', req).then(res => res.data)
    },
    /**
     * 通过工单号查找任务号
     * @returns {Promise<any>}
     */
    getTaskNumber() {
      this.isLoading = true
      let req = {
        method: 'get',
        url: `${IgetTaskNumber}/${this.keyword.toUpperCase()}`,
        noCommit: true
      }
      return this.$store.dispatch('saveData', req)
        .then(res => res.data)
        .catch(() => {
          return {
            taskNumber: ''
          }
        })
        .finally(() => {
          this.isLoading = false
        })
    },
  }
}

export default TreeService
